package microcredit.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JToggleButton;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import microcredit.Version;
import microcredit.gui.action.AboutAction;
import microcredit.gui.action.ExitAction;
import microcredit.gui.action.ExportAction;
import microcredit.gui.action.GroupsAction;
import microcredit.gui.action.ImportAction;
import microcredit.gui.action.MembershipAction;
import microcredit.gui.action.ReportsAction;
import microcredit.gui.action.TrainingsAction;
import microcredit.gui.base.BaseFrame;
import microcredit.gui.util.ColorConstants;
import javax.swing.JToolBar;

public class MainFrame extends BaseFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JPanel mainPanel;
	private JLabel logoLabel;
	private JLabel mainTitleLabel;
	private JLabel subTitleLabel;
	private JToggleButton membershipButton;
	private JToggleButton groupsButton;
	private JToggleButton trainingsButton;
	private JToggleButton reportsButton;
	private JButton importButton;
	private JButton ExportButton;
	private JButton aboutButton;
	private MembershipAction membershipAction;
	private GroupsAction groupsAction;
	private TrainingsAction trainingsAction;
	private ReportsAction reportsAction;
	private ImportAction importAction;
	private ExportAction exportAction;
	private AboutAction aboutAction;
	private JButton exitButton;
	private ExitAction exitAction;
	private JPanel panel;
	private JSeparator separator_1;
	private JPanel panel_1;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JToolBar titleBarPanel;
	private JPanel viewPanel;
	
	private MainFrameController controller;

	/**
	 * Create the frame.
	 */
	public MainFrame(MainFrameController controller) {
		this.controller = controller;
		this.controller.setMainFrame(this);
		setModalExclusionType(ModalExclusionType.NO_EXCLUDE);
		initialize();
		initializeKeyBindings();
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				getMembershipButton().setSelected(true);
			}
		});
	}
	
	protected void initializeKeyBindings() {
		initializeButton(getMembershipAction(), getMembershipButton());
		initializeButton(getGroupsAction(), getGroupsButton());
		initializeButton(getTrainingsAction(), getTrainingsButton());
		initializeButton(getReportsAction(), getReportsButton());
		initializeBinding(getImportAction());
		initializeBinding(getExportAction());
		initializeBinding(getAboutAction());
		initializeBinding(getExitAction());
	}
	
	protected void initializeButton(Action action, final JToggleButton b) {
		KeyStroke stroke = (KeyStroke)action.getValue(Action.ACCELERATOR_KEY);
		if (stroke != null) {
			getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, action.getClass().getName());
			getRootPane().getActionMap().put(action.getClass().getName(), new AbstractAction() {
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public void actionPerformed(ActionEvent e) {
					b.doClick();
				}
			});
		}
	}
	
	public MainFrameController getController() {
		return controller;
	}
	
	protected void initializeBinding(Action action) {
		KeyStroke stroke = (KeyStroke)action.getValue(Action.ACCELERATOR_KEY);
		if (stroke != null) {
			getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, action.getClass().getName());
			getRootPane().getActionMap().put(action.getClass().getName(), action);
		}
	}
	
	public void addTitleSuffix(String suffix) {
		if (suffix == null) {
			setTitle("Microcredit v "+Version.VERSION);
		} else{
			setTitle("Microcredit v "+Version.VERSION+" - "+suffix);
		}
		
	}
	
	private void initialize() {
		addTitleSuffix(null);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				exitButton.doClick(0);
			}
		});
		setBounds(100, 100, 756, 538);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(0, 0, 5, 0));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getMainPanel(), BorderLayout.CENTER);
		GridBagConstraints gbc_11 = new GridBagConstraints();
		gbc_11.gridheight = 2;
		gbc_11.gridwidth = 2;
		gbc_11.fill = GridBagConstraints.BOTH;
		gbc_11.gridx = 0;
		gbc_11.gridy = 0;
		getMainPanel().add(getPanel(), gbc_11);
	}

	private JPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new JPanel();
			mainPanel.setName("mainPanel");
			GridBagLayout gridBagLayout = new GridBagLayout();
			gridBagLayout.columnWidths = new int[]{0, 0, 0};
			gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
			gridBagLayout.columnWeights = new double[]{0.0, 1.0, 1.0E-4};
			gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 1.0E-4};
			mainPanel.setLayout(gridBagLayout);
			mainPanel.setName("panel");
			GridBagConstraints gbc_1 = new GridBagConstraints();
			gbc_1.fill = GridBagConstraints.BOTH;
			gbc_1.gridx = 1;
			gbc_1.gridy = 2;
			mainPanel.add(getTitleBarPanel(), gbc_1);
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.weighty = 1.0;
			gbc.weightx = 1.0;
			gbc.fill = GridBagConstraints.BOTH;
			gbc.gridx = 1;
			gbc.gridy = 3;
			mainPanel.add(getViewPanel(), gbc);
		}
		return mainPanel;
	}
	private JLabel getLogoLabel() {
		if (logoLabel == null) {
			logoLabel = new JLabel("");
			logoLabel.setIcon(new ImageIcon(MainFrame.class.getResource("/microcredit/icons/x48/environment_add.png")));
			logoLabel.setName("logoLabel");
			logoLabel.setName("label");
		}
		return logoLabel;
	}
	private JLabel getMainTitleLabel() {
		if (mainTitleLabel == null) {
			mainTitleLabel = new JLabel("Microcredit");
			mainTitleLabel.setFont(mainTitleLabel.getFont().deriveFont(mainTitleLabel.getFont().getStyle() | Font.BOLD, 18f));
			mainTitleLabel.setName("mainTitleLabel");
			mainTitleLabel.setName("label");
		}
		return mainTitleLabel;
	}
	private JLabel getSubTitleLabel() {
		if (subTitleLabel == null) {
			subTitleLabel = new JLabel("v "+Version.VERSION);
			subTitleLabel.setName("subTitleLabel");
			subTitleLabel.setName("label");
		}
		return subTitleLabel;
	}
	private JToggleButton getMembershipButton() {
		if (membershipButton == null) {
			membershipButton = new JToggleButton("");
			membershipButton.setFocusable(false);
			membershipButton.setRequestFocusEnabled(false);
			membershipButton.setMnemonic('M');
			membershipButton.setBorderPainted(false);
			membershipButton.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					membershipButtonItemStateChanged(e);
				}
			});
			buttonGroup.add(membershipButton);
			membershipButton.setAction(getMembershipAction());
			membershipButton.setName("membershipButton");
			membershipButton.setPreferredSize(new Dimension(membershipButton.getPreferredSize().width, membershipButton.getPreferredSize().height+5));
		}
		return membershipButton;
	}
	private JToggleButton getGroupsButton() {
		if (groupsButton == null) {
			groupsButton = new JToggleButton("New button");
			groupsButton.setFocusable(false);
			groupsButton.setRequestFocusEnabled(false);
			groupsButton.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					groupsButtonItemStateChanged(e);
				}
			});
			buttonGroup.add(groupsButton);
			groupsButton.setAction(getGroupsAction());
			groupsButton.setName("groupsButton");
			groupsButton.setPreferredSize(new Dimension(groupsButton.getPreferredSize().width, groupsButton.getPreferredSize().height+5));
		}
		return groupsButton;
	}
	private JToggleButton getTrainingsButton() {
		if (trainingsButton == null) {
			trainingsButton = new JToggleButton("New button");
			trainingsButton.setFocusable(false);
			trainingsButton.setRequestFocusEnabled(false);
			trainingsButton.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					trainingsButtonItemStateChanged(e);
				}
			});
			buttonGroup.add(trainingsButton);
			trainingsButton.setAction(getTrainingsAction());
			trainingsButton.setName("trainingsButton");
			trainingsButton.setPreferredSize(new Dimension(trainingsButton.getPreferredSize().width, trainingsButton.getPreferredSize().height+5));
		}
		return trainingsButton;
	}
	private JToggleButton getReportsButton() {
		if (reportsButton == null) {
			reportsButton = new JToggleButton("New button");
			reportsButton.setFocusable(false);
			reportsButton.setRequestFocusEnabled(false);
			reportsButton.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					reportsButtonItemStateChanged(e);
				}
			});
			buttonGroup.add(reportsButton);
			reportsButton.setAction(getReportsAction());
			reportsButton.setName("reportsButton");
			reportsButton.setPreferredSize(new Dimension(reportsButton.getPreferredSize().width, reportsButton.getPreferredSize().height+5));
		}
		return reportsButton;
	}
	private JButton getImportButton() {
		if (importButton == null) {
			importButton = new JButton("New button");
			importButton.setRequestFocusEnabled(false);
			importButton.setFocusable(false);
			importButton.setHorizontalAlignment(SwingConstants.LEFT);
			importButton.setAction(getImportAction());
			importButton.setName("importButton");
		}
		return importButton;
	}
	private JButton getExportButton() {
		if (ExportButton == null) {
			ExportButton = new JButton("New button");
			ExportButton.setRequestFocusEnabled(false);
			ExportButton.setFocusable(false);
			ExportButton.setHorizontalAlignment(SwingConstants.LEFT);
			ExportButton.setAction(getExportAction());
			ExportButton.setName("ExportButton");
		}
		return ExportButton;
	}
	private JButton getAboutButton() {
		if (aboutButton == null) {
			aboutButton = new JButton("New button");
			aboutButton.setRequestFocusEnabled(false);
			aboutButton.setFocusable(false);
			aboutButton.setHorizontalAlignment(SwingConstants.LEFT);
			aboutButton.setAction(getAboutAction());
			aboutButton.setName("aboutButton");
		}
		return aboutButton;
	}
	private MembershipAction getMembershipAction() {
		if (membershipAction == null) {
			membershipAction = new MembershipAction();
		}
		return membershipAction;
	}
	private GroupsAction getGroupsAction() {
		if (groupsAction == null) {
			groupsAction = new GroupsAction();
		}
		return groupsAction;
	}
	private TrainingsAction getTrainingsAction() {
		if (trainingsAction == null) {
			trainingsAction = new TrainingsAction();
		}
		return trainingsAction;
	}
	private ReportsAction getReportsAction() {
		if (reportsAction == null) {
			reportsAction = new ReportsAction();
		}
		return reportsAction;
	}
	private ImportAction getImportAction() {
		if (importAction == null) {
			importAction = new ImportAction(this);
		}
		return importAction;
	}
	private ExportAction getExportAction() {
		if (exportAction == null) {
			exportAction = new ExportAction(this);
		}
		return exportAction;
	}
	private AboutAction getAboutAction() {
		if (aboutAction == null) {
			aboutAction = new AboutAction();
		}
		return aboutAction;
	}
	private JButton getExitButton() {
		if (exitButton == null) {
			exitButton = new JButton("New button");
			exitButton.setRequestFocusEnabled(false);
			exitButton.setFocusable(false);
			exitButton.setHorizontalAlignment(SwingConstants.LEFT);
			exitButton.setAction(getExitAction());
			exitButton.setName("exitButton");
		}
		return exitButton;
	}
	private ExitAction getExitAction() {
		if (exitAction == null) {
			exitAction = new ExitAction();
		}
		return exitAction;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			
			panel.setBackground(ColorConstants.TITLE);
			GridBagLayout gridBagLayout = new GridBagLayout();
			gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0};
			gridBagLayout.rowHeights = new int[]{0, 0, 0};
			gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
			gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0E-4};
			panel.setLayout(gridBagLayout);
			GridBagConstraints gbc_5 = new GridBagConstraints();
			gbc_5.gridheight = 2;
			gbc_5.insets = new Insets(0, 5, 0, 5);
			gbc_5.gridx = 0;
			gbc_5.gridy = 0;
			panel.add(getPanel_1(), gbc_5);
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.anchor = GridBagConstraints.NORTHEAST;
			gbc.insets = new Insets(5, 0, 5, 5);
			gbc.gridx = 1;
			gbc.gridy = 0;
			panel.add(getImportButton(), gbc);
			GridBagConstraints gbc_1 = new GridBagConstraints();
			gbc_1.anchor = GridBagConstraints.NORTHEAST;
			gbc_1.insets = new Insets(5, 0, 5, 5);
			gbc_1.gridx = 2;
			gbc_1.gridy = 0;
			panel.add(getExportButton(), gbc_1);
			GridBagConstraints gbc_4 = new GridBagConstraints();
			gbc_4.fill = GridBagConstraints.BOTH;
			gbc_4.insets = new Insets(0, 0, 5, 5);
			gbc_4.gridx = 3;
			gbc_4.gridy = 0;
			panel.add(getSeparator_1(), gbc_4);
			GridBagConstraints gbc_2 = new GridBagConstraints();
			gbc_2.anchor = GridBagConstraints.NORTH;
			gbc_2.insets = new Insets(5, 0, 5, 5);
			gbc_2.gridx = 4;
			gbc_2.gridy = 0;
			panel.add(getAboutButton(), gbc_2);
			GridBagConstraints gbc_3 = new GridBagConstraints();
			gbc_3.insets = new Insets(5, 0, 5, 0);
			gbc_3.anchor = GridBagConstraints.NORTH;
			gbc_3.gridx = 5;
			gbc_3.gridy = 0;
			panel.setName("panel");
			panel.add(getExitButton(), gbc_3);
		}
		return panel;
	}
	private JSeparator getSeparator_1() {
		if (separator_1 == null) {
			separator_1 = new JSeparator();
			separator_1.setOrientation(SwingConstants.VERTICAL);
			separator_1.setName("separator_1");
		}
		return separator_1;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setOpaque(false);
			GridBagLayout gridBagLayout = new GridBagLayout();
			gridBagLayout.columnWidths = new int[]{0, 0, 0};
			gridBagLayout.rowHeights = new int[]{0, 0, 0};
			gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0E-4};
			gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0E-4};
			panel_1.setLayout(gridBagLayout);
			GridBagConstraints gbc = new GridBagConstraints();
			gbc.gridheight = 2;
			gbc.insets = new Insets(0, 0, 5, 5);
			gbc.gridx = 0;
			gbc.gridy = 0;
			panel_1.add(getLogoLabel(), gbc);
			GridBagConstraints gbc_1 = new GridBagConstraints();
			gbc_1.insets = new Insets(10, 0, 0, 0);
			gbc_1.anchor = GridBagConstraints.SOUTH;
			gbc_1.gridx = 1;
			gbc_1.gridy = 0;
			panel_1.add(getMainTitleLabel(), gbc_1);
			GridBagConstraints gbc_2 = new GridBagConstraints();
			gbc_2.anchor = GridBagConstraints.NORTHWEST;
			gbc_2.gridx = 1;
			gbc_2.gridy = 1;
			panel_1.add(getSubTitleLabel(), gbc_2);
			panel_1.setName("panel_1");
		}
		return panel_1;
	}
	private JToolBar getTitleBarPanel() {
		if (titleBarPanel == null) {
			titleBarPanel = new JToolBar();
			titleBarPanel.setFloatable(false);
			titleBarPanel.setRollover(true);
			titleBarPanel.setOpaque(true);
			titleBarPanel.setBackground(ColorConstants.TITLE_BAR);
			titleBarPanel.setBorderPainted(true);
			MatteBorder border = new MatteBorder(1, 0, 1, 0, (Color) Color.GRAY);
			EmptyBorder emptyBorder = new EmptyBorder(3, 10, 2, 10);
			titleBarPanel.setBorder(new CompoundBorder(border, emptyBorder));
			titleBarPanel.add(getMembershipButton());
			titleBarPanel.add(getGroupsButton());
			titleBarPanel.add(getTrainingsButton());
			titleBarPanel.add(getReportsButton());
		}
		return titleBarPanel;
	}
	
	public  JPanel getViewPanel() {
		if (viewPanel == null) {
			viewPanel = new JPanel();
			viewPanel.setLayout(new BorderLayout(0, 0));
			viewPanel.setName("viewPanel");
		}
		return viewPanel;
	}

	protected void membershipButtonItemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			controller.selectMemberShip();
		}

	}

	protected void groupsButtonItemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			controller.selectGroups();
		}
	}

	protected void trainingsButtonItemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			controller.selectTrainings();
		}
	}

	protected void reportsButtonItemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			controller.selectReports();
		}
	}
}
